/**
 * 
 */
package es.aradesh.ministryassist.gui;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;
import android.widget.ToggleButton;
import es.aradesh.ministryassist.R;
import es.aradesh.ministryassist.db.DbAdapter;
import es.aradesh.ministryassist.logic.ExportTask;
import es.aradesh.ministryassist.logic.ExportTaskParams;
import es.aradesh.ministryassist.logic.ExportTaskResult;
import es.aradesh.ministryassist.logic.ExportTask.OnExportFinishedListener;
import es.aradesh.ministryassist.logic.ministry.Territory;
import es.aradesh.ministryassist.logic.ministry.TerritoryBuilder;
import es.aradesh.ministryassist.utils.Notifications;

/**
 * @author xmorales
 * 
 */
public class ExportActivity extends ListActivity {


	private DbAdapter mDb;
	private ListView activityList;
	private RadioButton importer;
	private RadioButton exporter;
	private static final String exportFileName = "/sdcard/MinistryAssistant/exported.xml";

	ExportTask mExportTask;
	ExportTaskParams mExportParams;
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.export);
		
		mDb = new DbAdapter(this);
		mDb.open();

		Button launchButton = (Button) findViewById(R.id.ExportButtonGo);
		ToggleButton allButton = (ToggleButton)findViewById(R.id.ExportToggleAll); 
		activityList = this.getListView();
		importer = (RadioButton) findViewById(R.id.ExportRadioImport);
		exporter = (RadioButton) findViewById(R.id.ExportRadioExport);
		
		activityList.setVisibility(View.GONE);
		allButton.setVisibility(View.GONE);
		importer.setChecked(true);
		
		List<Territory> terris = TerritoryBuilder.getTerritoryList(this.mDb);
		mDb.close();
		if (terris.isEmpty()) {
			// Show a default message
			setListAdapter(new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, new String[] { this
							.getString(R.string.noElements) }));
		} else {
			setListAdapter(new ArrayAdapter<Territory>(this,
					R.layout.export_row, R.id.ExportCheckBox, terris));
		}
		mExportTask = new ExportTask(this);
		mExportParams = new ExportTaskParams();
		
		mExportTask.setOnExportFinishedListener(new OnExportFinishedListener() {
			
			public void onExportFinished(ExportTaskResult result) {
			//	dismissDialog(DIALOG_PROGRESS);
				
				if (result.successful) {
		            Toast.makeText(ExportActivity.this, getString(R.string.exportOK, result.file), Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(ExportActivity.this, getString(R.string.exportError), Toast.LENGTH_LONG).show();
				}
				finish();
			}
		});
		launchButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				if (exporter.isChecked()) {
					List<Territory> selected = new ArrayList<Territory>();
					for (int i = 0; i < activityList.getChildCount(); i++) {
						if (((CheckBox)activityList.getChildAt(i)).isChecked())
							selected.add((Territory) activityList
									.getItemAtPosition(i));
						
					}
					mExportParams.file = new File(exportFileName);
					mExportParams.terris = selected;
					mExportTask.execute(mExportParams);
//					exportXML("", selected);
				}else{
					Notifications.infoDialog(view.getContext(), "Error", getString(R.string.importError));
				}
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	private void importXML(String file) {
		File f = new File(file);
		if (f.exists() && f.canRead()) {

		} else {
			Notifications.infoDialog(this, "Error",getString(R.string.importError));
		}
	}

	public void onRadioButtonClick(View view){
		if(view.getId() == R.id.ExportRadioImport) {
			((RadioButton)findViewById(R.id.ExportRadioExport)).setChecked(false);
			activityList.setVisibility(View.GONE);
			findViewById(R.id.ExportToggleAll).setVisibility(View.GONE);
		}
		if(view.getId() == R.id.ExportRadioExport) {
			((RadioButton)findViewById(R.id.ExportRadioImport)).setChecked(false);
			activityList.setVisibility(View.VISIBLE);
			//findViewById(R.id.ExportToggleAll).setVisibility(View.VISIBLE);
		}
	}
}
